package com.nh.reception.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nh.common.pojo.Log;
import com.nh.reception.mapper.ReceptionMapper;
import com.nh.reception.service.ReceptionService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.CompletableFuture;

/**
 * @Author: LaoCao
 * @Date: 2023/3/16 16:40
 * @Version: 1.0
 **/
@Service
public class ReceptionServiceImpl implements ReceptionService {
    private static final String LOG_ID = "log:id";
    @Resource
    private ReceptionMapper receptionMapper;
    @Resource
    private AbstractExecutorService threadPoolExecutor;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void batchLogs(List<Log> logList) {
        CompletableFuture.runAsync(() -> {
            //若数据量超过1000，则进行分批操作
            int maxNum = logList.size();
            int num = 0;
            while (maxNum >= 0) {
                List<Log> logs;
                if (maxNum >= 1000) {
                    logs = logList.subList(num, num += 1000);
                    maxNum -= 1000;
                } else {
                    logs = logList.subList(logList.size() - maxNum,logList.size());
                    maxNum -= logList.size();
                }
                receptionMapper.batchLogs(logs);
            }
        }, threadPoolExecutor);
    }

    @Override
    public List<Long> findLogIdList(Long logId, String date) {
        /*select * from log
          where log_id >= 20230318794508 and log_date_time like '2023-03-19%'
        */
        return receptionMapper.selectLogIdListByLogIdAndDate(logId,date);
//                .selectList(new QueryWrapper<Log>()
//                .ge("log_id", logId))
//                .likeRight("log_date_time", date)
//                .ge("log_id", logId))
//                .stream()
//                .map(Log::getLogId)
//                .collect(Collectors.toList());

//        List<Long> logIdList;
//        //先从redis中查询是否有缓存
//        logIdList = (List<Long>) redisTemplate.opsForHash().get(LOG_ID, date);
//        if (logIdList != null) {
//            return logIdList;
//        }
//        //从数据库中获取，并存入redis缓存
//        logIdList =
//                receptionMapper
//                        .selectList(new QueryWrapper<Log>().likeRight("log_date_time", date))
//                        .stream()
//                        .map(Log::getLogId)
//                        .collect(Collectors.toList());
//        //设置过期时间
//        int time = 60 * 60 * 24;
//        if (logIdList.size() == 0) {
//            time = 60;
//        }
//        redisTemplate.opsForHash().put(LOG_ID, date, logIdList);
//        redisTemplate.expire(LOG_ID, time, TimeUnit.SECONDS);
//        return logIdList;
    }

    @Override
    public IPage<Log> findPageLog(IPage<Log> page, String date) {
        return receptionMapper
                .selectPage(page, new QueryWrapper<Log>()
                        .likeRight(date != null, "log_date_time", date));
    }
}
